package jnpf.service.impl;


import com.fasterxml.jackson.databind.ObjectMapper;
import jnpf.common.enums.ragFlow.RagFlowApiEnum;
import jnpf.model.ragFlow.chunk.*;
import jnpf.model.ragFlow.common.RagFlowApiResult;
import jnpf.service.RagChunksService;
import jnpf.util.RagFlowApiReqUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import jnpf.util.UserProvider;

import static jnpf.util.ResultHandleUtil.txtConvertObj;


@Service
public class RagChunksServiceImpl implements RagChunksService {
    @Value("${ragflow.url:}")
    public String API_BASE_URL;
    @Value("${ragflow.knowApiKey}")
    public String API_KEY;
    @Autowired
    public UserProvider userProvider;



    @Override
    public RagFlowApiResult addChunks(String datasetId, String documentId, AddChunksRequest request) throws Exception {
        Map<String, String> pathParams = new HashMap<>();
        pathParams.put("dataset_id", datasetId);
        pathParams.put("document_id", documentId);
        return txtConvertObj(RagFlowApiReqUtil.sendRequest(API_BASE_URL, API_KEY, RagFlowApiEnum.ADD_CHUNKS, pathParams, request,userProvider.get().getUserId()), AddChunkResp.class);
    }

    @Override
    public RagFlowApiResult listChunks(String datasetId, String documentId, ListChunksRequest request) throws Exception {
        Map<String, String> pathParams = new HashMap<>();
        pathParams.put("dataset_id", datasetId);
        pathParams.put("document_id", documentId);
        Map<String, String> queryParams = new HashMap<>();
        if (request.getKeywords() != null) {
            queryParams.put("keywords", request.getKeywords());
        }
        if (request.getPage() != null) {
            queryParams.put("page", request.getPage().toString());
        }
        if (request.getPage_size() != null) {
            queryParams.put("page_size", request.getPage_size().toString());
        }
        if (request.getId() != null) {
            queryParams.put("id", request.getId());
        }
        return txtConvertObj(RagFlowApiReqUtil.sendRequest(API_BASE_URL, API_KEY, RagFlowApiEnum.LIST_CHUNKS, pathParams, queryParams,userProvider.get().getUserId()), DocumentChunksResp.class);
    }

    @Override
    public RagFlowApiResult deleteChunks(String datasetId, String documentId, DeleteChunksRequest request) throws Exception {
        Map<String, String> pathParams = new HashMap<>();
        pathParams.put("dataset_id", datasetId);
        pathParams.put("document_id", documentId);
        return txtConvertObj(RagFlowApiReqUtil.sendRequest(API_BASE_URL, API_KEY, RagFlowApiEnum.DELETE_CHUNKS, pathParams, request, userProvider.get().getUserId()),AddChunkResp.class);
    }

    @Override
    public RagFlowApiResult updateChunk(String datasetId, String documentId, String chunkId, UpdateChunkRequest request) throws Exception {
        Map<String, String> pathParams = new HashMap<>();
        pathParams.put("dataset_id", datasetId);
        pathParams.put("document_id", documentId);
        pathParams.put("chunk_id", chunkId);
        return txtConvertObj(RagFlowApiReqUtil.sendRequest(API_BASE_URL, API_KEY, RagFlowApiEnum.UPDATE_CHUNK, pathParams, request, userProvider.get().getUserId()),AddChunkResp.class);
    }

    @Override
    public RagFlowApiResult retrieveChunks(RetrieveChunksRequest request) throws Exception {
        return txtConvertObj(RagFlowApiReqUtil.sendRequest(API_BASE_URL, API_KEY, RagFlowApiEnum.RETRIEVE_CHUNKS, new HashMap<>(), request, userProvider.get().getUserId()),SearchResult.class);
    }


}
